var redis = "redis"
/**
 * 将 key 和 value 存到 Storage 中
 * key 键
 * value 值
 * time 有效期（单位秒）
 */
function put(key, value, time) {
    wx.setStorageSync(key, value);
    var seconds = parseInt(time);
    if (seconds > 0) {
        var newtime = Date.parse(new Date());
        newtime = newtime / 1000 + seconds;
        wx.setStorageSync(key + "_" + redis, newtime + "");
    } else {
        wx.removeStorageSync(key + "_" + redis);
    }
}
/**
 * 从 Storage 中通过 key 获取 value
 * key 键
 */
function get(key) {
    var deadtime = parseInt(wx.getStorageSync(key + "_" + redis));
    if (deadtime) {
        if (parseInt(deadtime) < Date.parse(new Date()) / 1000) {
            clear();
            return null;
        }
    }
    var res = wx.getStorageSync(key)
    if (res) {
        return res;
    } else {
        return null;
    }
}

/**
 * 通过 key 删除 key-value
 */
function remove(key) {
    wx.removeStorageSync(key);
    wx.removeStorageSync(key + "_" + redis);
}

/**
 * 清除所有 key-value
 */
function clear() {
    wx.clearStorageSync();
}

export default {
    put,
    get,
    remove,
    clear
}